package cn.px.rest.controller.sb;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.sb.SbMeasureLogDTO;
import cn.px.dao.entity.sb.SbMeasureLog;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.sb.ExportSbMeasureLogVO;
import cn.px.dao.vo.query.sb.SbMeasureLogVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.sb.SbMeasureLogService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 设备计量记录 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sb/measure-logs")
public class SbMeasureLogController {

  private final SbMeasureLogService checkStandardService;

    /**
     * 通过id查询标准
     *
     * @param id 主键
     * @return R
     */
    @GetMapping("/{id}")
    public R<SbMeasureLog> getById(@PathVariable("id") String id){
        return new R<>(checkStandardService.getModelById(id));
    }

  /**
   * 新增记录
   *
   * @param checkStandardDTO 设备计量记录DTO
   * @return R
   */
  @SysLog("新增设备计量记录")
  @PostMapping
  public R save(@Validated({AddGroup.class}) @RequestBody SbMeasureLogDTO checkStandardDTO) {
      return new R<>(checkStandardService.saveModelByDTO(checkStandardDTO));
  }

  /**
   * 修改记录
   *
   * @param checkStandardDTO 设备计量记录DTO
   * @return R
   */
  @SysLog("修改设备计量记录")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('measure-log-edit')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody SbMeasureLogDTO checkStandardDTO) {
      checkStandardService.modModelByDTO(checkStandardDTO);
      return new R<>();
  }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除设备计量记录")
  @DeleteMapping("/{id}")
  public R removeById(@PathVariable String id){
    checkStandardService.deleteByPrimaryKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除设备计量记录")
    @DeleteMapping("")
    public R removeIds(@RequestBody List<String> ids){
        checkStandardService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param checkStandardDTO 设备计量记录DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<SbMeasureLog>> query(SbMeasureLogDTO checkStandardDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      return new R<>(checkStandardService.selectPageInfo(checkStandardDTO, pageNum, pageSize));
  }

  /**
   * 获取列表
   *
   * @param checkStandardDTO 设备计量记录DTO
   * @return R
   */
  @GetMapping("")
  public R query(SbMeasureLogDTO checkStandardDTO) {
      return new R<>(checkStandardService.selectList(checkStandardDTO));
  }

  /**
     * 设备计量记录导出
     *
     * @param checkStandardDTO 设备计量记录DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("计量设备检定记录导出")
  @PreAuthorize("@pms.hasPermission('measure-log-export') or @pms.hasPermission('check-polling-standards-export')")
  public void export(HttpServletResponse response, SbMeasureLogDTO checkStandardDTO) {
    List<SbMeasureLogVO> list = checkStandardService.selectList(checkStandardDTO);
    ExcelUtil.exportResponseDict(response, ExportSbMeasureLogVO.class, BeanConverterUtil.copyListProperties(list, ExportSbMeasureLogVO.class), "计量设备检定记录");
  }

}
